package com.mall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.address.entity.Address;
import com.mall.address.service.AddressService;
import com.mall.admin.interfaces.Permission;
import com.mall.comment.entity.Comment;
import com.mall.comment.service.CommentService;
import com.mall.commondataformat.R;
import com.mall.dingdan.entity.Dingdan;
import com.mall.dingdan.service.DingdanService;
import com.mall.inform.entity.Inform;
import com.mall.inform.service.InformService;
import com.mall.product.entity.Product;
import com.mall.product.service.ProductService;
import com.mall.user.entity.User;
import com.mall.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

import static com.mall.commonutils.ResultCode.PAGE_LIMIT;
import static com.mall.commonutils.ResultCode.USER_NOT_LOGIN;
import static com.mall.commonutils.SessionStaticMes.USER_IN_SESSION;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-05-04
 */
@RestController
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private InformService informService;
    @Autowired
    private DingdanService dingdanService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    //查询所有的商品
    @ApiOperation("查询所有商品")
    @GetMapping("/getProductList/{page}")
    public R getProductList(@ApiParam(name = "page", value = "当前页码", required = true)
                            @PathVariable Integer page) {
        Page<Product> productPage = new Page<>(page, PAGE_LIMIT);

        QueryWrapper<Product> wrapper = new QueryWrapper<>();

        productService.page(productPage, wrapper);

        List<Product> list = productService.list(null);

        return R.ok().data("Product", list);
    }

    //查询当前商品评论
    @ApiOperation("查询当前商品评论")
    @GetMapping("/getCommentList")
    public R getCommentList(Long id) {
        List<Comment> list = commentService.list(null);
        Comment comment;
        for(int i=0;i<list.size();i++){
            comment=new Comment();
            comment=list.get(i);
            if(!comment.getShangpinId().equals(id)){
                list.remove(i);
                i=i-1;
            }
        }
        if(list.size()==0){
            return R.ok().message("该商品无评论！");
        }else {
            return R.ok().data("comment", list);
        }
    }

    //增加商品评论
    @ApiOperation("增加商品评论")
    @PostMapping("/addComment")
    public R addComment(HttpSession session, long product_id, String content) {

        User user = (User) session.getAttribute(USER_IN_SESSION);
        Comment comment = new Comment();
        Product product = productService.getById(product_id);
        comment.setUserId(user.getId());
        comment.setShangpinId(product_id);
        comment.setContent(content);
        comment.setUsername(user.getUsername());
        comment.setProductName(product.getName());

        boolean res = commentService.save(comment);
        if (res) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //查询最新公告
    @ApiOperation("查询公告")
    @GetMapping("/getInform")
    public R getInform() {
        List<Inform> list;
        list = informService.list(null);
        Inform inform = list.get(0);

        return R.ok().data("content", inform.getContent());
    }
     /*
    //查询商品状态
    @ApiOperation("查询商品状态")
    @GetMapping("/getStatus")
    public R getStatus(long ShangPin_id){
        com.mall.dingdan.entity.Dingdan order = dingdanService.getById(ShangPin_id);
        return R.ok().data("isPay", order.getIsPay()).data("isSend", order.getIsSend()).data("isAccept", order.getIsAccept());
    }*/

    //购买商品
    @ApiOperation("购买商品")
    @PostMapping("/buy")
    public R buy(long product_id, int address_id, HttpSession session) {

        User user = (User) session.getAttribute(USER_IN_SESSION);
        if (user == null) {
            return R.error().message(USER_NOT_LOGIN);
        }
        System.out.println("@@@@"+product_id+"***"+address_id);
        Dingdan order = new Dingdan();
        Address address = new Address();
        address = addressService.getById(address_id);
        Product product = new Product();
        product = productService.getById(product_id);


        if (product.getPrice() <= user.getMoney()) {
            //更新订单
            user.setMoney(user.getMoney() - product.getPrice());
            product.setCount(product.getCount() - 1);
            product.setNumber(product.getNumber()+1);

            order.setUserId(user.getId());
            order.setShangpinId(product.getId());
            order.setAccpetAddress(address.getContent());
            order.setIsPay(1);
            order.setIsSend(0);
            order.setIsAccept(0);
            order.setUsername(user.getUsername());
            order.setProductName(product.getName());
            order.setAcceptName(address.getName());
            order.setAcceptPhone(address.getPhone());
            order.setAccpetAddress(address.getContent());

            boolean res_dingdan = dingdanService.save(order);
            boolean res_user = userService.updateById(user);
            boolean res_ShangPin = productService.updateById(product);

            if (res_ShangPin && res_user && res_dingdan) {
                return R.ok();
            } else {
                return R.error().message("数据更新失败");
            }
        } else {
            return R.error().message("账户余额不足");
        }
    }

    //查询用户购物车
    public R carList (HttpSession session) {

        
        return R.ok();
    }
}